package com.bihu.bihudemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bihu.bihudemo.dao.QuestionDao;
import com.bihu.bihudemo.dao.TagArticleLinkDao;
import com.bihu.bihudemo.entity.Comment;
import com.bihu.bihudemo.entity.Question;
import com.bihu.bihudemo.entity.Spot;
import com.bihu.bihudemo.entity.Tag;
import com.bihu.bihudemo.entity.vo.QuestionVo;
import com.bihu.bihudemo.exception.ErrorEnum;
import com.bihu.bihudemo.result.Result;
import com.bihu.bihudemo.service.QuestionService;
import com.bihu.bihudemo.service.RedisService;
import com.bihu.bihudemo.service.SpotService;
import com.bihu.bihudemo.service.TagService;

import com.bihu.bihudemo.utils.redis.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 文章(Question)表控制层
 *
 * @author makejava
 * @since 2020-07-15 17:10:00
 */
@RestController
@RequestMapping("question")
@Api(tags = "问题相关操作")
public class QuestionController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private QuestionService questionService;

    @Resource
    QuestionDao questionDao;

    @Resource
    TagArticleLinkDao tagArticleLinkDao;


    @Resource
    SpotService spotService;

    @Resource
    RedisService redisService;

    @Resource
    RedisUtil redisUtil;

    /**
     * 分页获取所有问题（带有标签）
     * @param page
     * @return
     */
    @GetMapping
    @ApiOperation("获取所有的问题（每个问题带有自己的标签）")
    public Result selectAll(Page<QuestionVo>page) {
        IPage<QuestionVo> pages = questionService.getAllQuestionWithTagsByPage(page);
        return Result.ok().put("page",pages);
    }

    /**
     * 根据标签的ID分类获取问题
     * @param page 分页条件
     * @param tid 标签ID
     * @return
     */

    @ApiOperation("根据标签ID获取问题")
    @GetMapping("/selectWithTagID")
    public Result selectWithTagID(Page<QuestionVo>page,@RequestParam("tid")Integer tid){
        IPage<QuestionVo> questionWithTagsByTagID = questionService.getQuestionWithTagsByTagID(page, tid);
        return Result.ok().put("page",questionWithTagsByTagID);
    }

    /**
     * 根据标题内容模糊查询
     * @param page 分页条件
     * @param title 标题内容
     * @return
     */
    @ApiOperation("根据问题标题模糊搜索")
    @GetMapping("/selectWithTitle")
    public Result selectWithTitle(Page<QuestionVo>page,@RequestParam("title")String title){
        IPage<QuestionVo> questionWithTagsByTagID = questionService.getQuestionWithTagsByTitle(page, title);
        return Result.ok().put("page",questionWithTagsByTagID);
    }

    /**
     * 用户获取自己关注过的问题
     * @param uid 用户ID
     * @return
     */
    @ApiOperation("根据用户ID，获取用户关注的问题")
    @GetMapping("/getfocusQuestion")
    public Result getfocusQuestion(int uid){
        List<Question> list = questionDao.getforkedQuestion(uid);
        return Result.ok().put("forkedQuestion",list);
    }


    /**
     *关注问题的controller
     * @param uid 用户ID
     * @param aid 文章ID
     * @return
     */
    @ApiOperation("问题关注操作")
    @PostMapping("/spotQuestion")
    public Result forkQuestion(int uid,int aid){

        QueryWrapper<Spot> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        wrapper.eq("aid",aid);
        Spot one = spotService.getOne(wrapper);
        if(one!=null){
            return Result.error(ErrorEnum.DUPLICATE_KEY);
        }else {
            Spot spot = new Spot();
            spot.setAid(aid);
            spot.setUid(uid);
            spotService.save(spot);
            return Result.ok();
        }
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        Question question = this.questionService.getById(id);
        return success(question);
    }


    @ApiOperation("根据ID拿到问题详情")
    @GetMapping("/getByID")
    public Result selectByID(@RequestParam("aid")int id){
        QuestionVo questionVo = questionDao.getOneByID(id);
        redisService.addViewNum(questionVo);
        List<Tag> tags = tagArticleLinkDao.getTagsByArticle(questionVo);
        questionVo.setTagList(tags);
        return Result.ok().put("question",questionVo);
    }
    /**
     * 新增数据
     *
     * @param question 实体对象
     * @return 新增结果
     */
    @ApiOperation("发布问题")
    @PostMapping
    public Result insert(@RequestBody Question question) {

        if(this.questionService.save(question)){
            return Result.ok().put("qid",question.getId());
        }else {
            return Result.error(ErrorEnum.UNKNOWN);
        }
    }


    @ApiOperation("查询指定用户的问题")
    @GetMapping("/getQuestionByUid")
    public Result GetQuestionByUser(int uid,int page){

        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        Page<Question> result = questionService.page(new Page<>(page, 6), wrapper);
        return Result.ok().put("result",result);
    }

    @ApiOperation("点赞问题")
    @PostMapping("/like")
    public Result like(@RequestParam("qid")int id){
        questionDao.likeQuestion(id);
        return Result.ok().put("msg","点赞成功");
    }

    /**
     * 修改数据
     *
     * @param question 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Question question) {
        return success(this.questionService.updateById(question));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.questionService.removeByIds(idList));
    }




}